package org.example.software.controller;

import org.example.software.entity.DTO.UserUpdate;
import org.example.software.entity.User;
import org.example.software.entity.VO.UserInfo;
import org.example.software.exception.MyException;
import org.example.software.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //用户注册
    @PostMapping("/register")
    public ResponseEntity<?> registerUser(@RequestBody User user) {
        try {
            boolean success = userService.register(user);
            if (success) {
                return ResponseEntity.ok("用户注册成功");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("用户注册失败");
            }
        } catch (MyException e) {
            // 捕获 MyException，自定义返回状态码和消息
            return ResponseEntity.status(e.getCode()).body(e.getMessage());
        }
        catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误");
        }
    }

    //用户登录
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody User user) {
        try {
            String token = userService.login(user);
            if (token != null) {
                return ResponseEntity.ok(token);
            }
            else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("用户登录失败");
            }
        } catch (MyException e) {
            // 捕获 MyException，自定义返回状态码和消息
            return ResponseEntity.status(e.getCode()).body(e.getMessage());
        }
        catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误");
        }
    }

    //用户信息展示
    @GetMapping("/info")
    public ResponseEntity<?> getUserInfo(@RequestHeader("Authorization") String token) {
        try {
            UserInfo userInfo = userService.getUserByToken(token);
            return ResponseEntity.ok(userInfo);
        } catch (MyException e) {
            // 捕获 MyException，自定义返回状态码和消息
            return ResponseEntity.status(e.getCode()).body(e.getMessage());
        }
        catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误");
        }
    }

    //用户信息修改
    @PutMapping("/update")
    public ResponseEntity<?> updateUser(
            @RequestHeader("Authorization") String token,
            @RequestBody UserUpdate userUpdate) {

        try {
            // 调用服务层方法进行用户信息更新
            boolean result = userService.updateUser(token, userUpdate);

            if (result) {
                return ResponseEntity.status(HttpStatus.OK).body("用户信息更新成功");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("用户信息更新失败");
            }
        } catch (MyException e) {
            // 捕获 MyException，自定义返回状态码和消息
            return ResponseEntity.status(e.getCode()).body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误");
        }
    }

    //用户密码修改
    @PutMapping("/changePassword")
    public ResponseEntity<?> changePassword(
            @RequestHeader("Authorization") String token,
            @RequestParam("oldPassword") String oldPassword,
            @RequestParam("newPassword") String newPassword) {

        try {
            // 调用服务层方法修改密码
            boolean isSuccess = userService.changePassword(token, oldPassword, newPassword);
            if (isSuccess) {
                return ResponseEntity.ok("密码修改成功");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("密码修改失败");
            }
        } catch (MyException e) {
            // 捕获 MyException，自定义返回状态码和消息
            return ResponseEntity.status(e.getCode()).body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误");
        }
    }

}


